package com.example.back.controller;

import com.example.back.common.GetNow;
import com.example.back.entity.Post;
import com.example.back.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
public class PostController {
    @Autowired
    private PostService postService;
    @Autowired
    private GetNow getNow;
    @PostMapping("/post")
    public int addPost(@RequestBody Post post){
       String time = getNow.getNow();
       post.setTime(time);
        return postService.addPost(post);
    }
    @DeleteMapping("/post/{id}")
    public int deletePost(@PathVariable("id") Integer id){
        return postService.deletePost(id);
    }
    @PutMapping("/post")
    public int updatePost(@RequestBody Post post){
        return postService.updatePost(post);
    }
    @GetMapping("/poster/{id}")
    public Post queryPostById(@PathVariable("id") Integer id){
        postService.addBrowse(id);
        return postService.queryPostById(id);
    }
    @GetMapping("/post")
    public ArrayList<Post> queryPost( Post post){
        return postService.queryPost(post);
    }
    @GetMapping("/top")
    public ArrayList<Post> getTopPostsByBrowse(){
        return postService.getTopPostsByBrowse();
    }
}
